package de.s9mtdev.lol_destiny_home;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.IRCEvent.Type;
import jerklib.listeners.IRCEventListener;

public class BuffView extends JPanel implements MouseListener, IRCEventListener {
	
	Session session;
	String sessionkey;
	String id;
	long when = 0;
	
	
	public BuffView(InSession is, String s) {
		session = is.getSession();
    	session.addIRCEventListener(this);
    	sessionkey = is.getSessionkey();
    	id = s;
    	this.addMouseListener(this);
	}
	
	
	public void paintComponent(Graphics g)
    {   
		g.setColor(Color.WHITE);
		g.fillRect(0,0,200,200);
    	Graphics2D g2d = (Graphics2D)g;
    	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    	
        long now = System.currentTimeMillis();
		Long timeleft = 300 - ((now - when) / 1000);
    	
    	if (id.contains("Red"))
    		g.setColor(Color.RED);
    	else
    		g.setColor(Color.BLUE);
    	
    	if (id.contains("Left")) {
    		g.fillRect(5, 0, 80, 40);
    		g.setColor(Color.WHITE);
    		g.setFont(new Font("Arial", 0, 12));
    		if (when == (long)0 || timeleft <= (long)0) {			
    			g.drawString(id, 8, 16);
    			g.drawString("unknown", 8, 32);
    		} else {
    			g.drawString(id, 8, 16);
    			g.drawString("re in " + timeleft.toString(), 8, 32);
    		}
    	}
    	else {
    		g.fillRect(80, 0, 80, 40);
    		g.setColor(Color.WHITE);
    		g.setFont(new Font("Arial", 0, 12));
    		if (when == (long)0 || timeleft <= (long)0) {			
    			g.drawString(id, 83, 16);
    			g.drawString("unknown", 83, 32);
    		} else {
    			g.drawString(id, 83, 16);
    			g.drawString("re in " + timeleft.toString(), 83, 32);
    		}
    	}
    }

	@Override
	public void receiveEvent(IRCEvent e) {
		if ( e.getType() == Type.CHANNEL_MESSAGE) {
			String d = e.getRawEventData();
			int dataStartsAt = d.lastIndexOf(":") +1;
			d = d.substring(dataStartsAt);
			if (d.startsWith("buff") && d.endsWith(id)) {
				when = System.currentTimeMillis();
				}
			}
	}		


	@Override
	public void mouseClicked(MouseEvent arg0) {
		when = System.currentTimeMillis();
		sendMsg("buff " + id);
		repaint();		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	private void sendMsg(String a) {
		session.sayChannel(session.getChannel("#dy_"+sessionkey), a);
	}

}
